/**
 * 
 */
package com.fitnessapp.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.fitnessapp.database.DatabaseHelper;
import com.fitnessapp.database.DatabaseStrings;
import com.fitnessapp.database.MySqlHelper;

/**
 * @author Brett
 *
 */
public class User {
	
	private List<Routine> routines = new LinkedList<Routine>();
	private Routine currentRoutine = null;
	private int userId;
	private String username;
	private int currentRoutineId = -1; //the index of the current routine in the routine list	
	
	public User() {

	}
	

	public void loadLocalUser(SQLiteDatabase db){
		String[] results = new String[] {
				DatabaseHelper.USER_TABLE_ID, DatabaseHelper.USER_TABLE_USERNAME,
				DatabaseHelper.USER_TABLE_CURRENT_ROUTINE};
		
		String where = DatabaseHelper.USER_TABLE_USERNAME + "='" + DatabaseHelper.LOCAL_USER + "'";
		
		Cursor cursor = db.query(DatabaseHelper.USER_TABLE, results, where, null, null, null, null);
		
		Log.d("", cursor.getCount() + "");
		Log.d("User.java", cursor.getColumnCount() + "");
		Log.d("", cursor.getColumnIndex(DatabaseHelper.USER_TABLE_ID) + "");
		
		String[] columns =  cursor.getColumnNames();
		for(int i = 0; i < cursor.getColumnCount(); i++){
			Log.d("",columns[i]);
		}
		
		cursor.moveToFirst();
		
		userId = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.USER_TABLE_ID));
		
		int currentRoutineCol = cursor.getColumnIndex(DatabaseHelper.USER_TABLE_CURRENT_ROUTINE);
		if( currentRoutineCol > -1){
			currentRoutineId = cursor.getInt(currentRoutineCol);
		}
		
		cursor.close();
		
		//now load all the routines
		loadRoutines(db);
		
	}

	private void loadRoutines(SQLiteDatabase db){
		String[] results = new String[] {DatabaseHelper.ROUTINE_TABLE_ID,
				DatabaseHelper.ROUTINE_TABLE_NAME, DatabaseHelper.ROUTINE_TABLE_SHARED_FLAG,
				DatabaseHelper.ROUTINE_TABLE_CREATION_DATE, DatabaseHelper.ROUTINE_TABLE_AUTHOR_NAME,
				DatabaseHelper.ROUTINE_TABLE_GOAL};		
		
		
		Cursor cursor = db.query(DatabaseHelper.ROUTINE_TABLE, results, null, null, null, null, null);
		routines.clear();
		
		if( cursor.moveToFirst() ){			
			do{
				Routine routine = new Routine();
				routine.loadFromCursor(cursor);
				
				if(routine.getId() == this.currentRoutineId){
					routine.loadExercises(db);
					currentRoutine = routine;
				}				
				
				routines.add(routine);
				
			}while(cursor.moveToNext());
		}
		
		cursor.close();	

	}
	
	public List<Map<String,String>> getRoutineData() { 
		List<Map<String, String>> routineData = null;
		
		if( routines != null ){
		//create a list with all the routines and keys we want to display
		 routineData = new ArrayList<Map<String,String>>(routines.size());
		
		for(Routine routine : routines){
			Map<String,String> data = new HashMap<String,String>();
			data.put(DatabaseHelper.ROUTINE_TABLE_NAME, routine.getName());
			data.put(DatabaseHelper.USER_TABLE_CURRENT_ROUTINE,( (routine == this.currentRoutine) ? "true": "false"));
			routineData.add(data);
			
		}
		}
		
		return routineData;
	}
	
	public Routine getCurrentRoutine() {
		return currentRoutine;
	}
	
	public int getId() { return userId; }


	public void addRoutine(Routine routine) {
		routines.add(routine);		
	}
	
	public void removeRoutine(String name, SQLiteDatabase db){		
	
		for(Routine r : routines)
		{
			if( name.equals(r.getName())){
				routines.remove(r);
				r.deleteFromDB(db);
				break;
			}
		}
	}


	public void setCurrentRoutine(String name) {
		for(Routine r : routines)
		{
			if( name.equals(r.getName())){
				currentRoutine = r;
				break;
			}
		}
	}


	public Routine getRoutine(String routineName) {
		Routine ret = null;
		for(Routine r : routines)
		{
			if( routineName.equals(r.getName())){
				ret = r;
				break;
			}
		}
		return ret;
	}



}
